package com.songlanyun.modules.skus.controller.admin;

import com.songlanyun.common.annotation.SysLog;
import com.songlanyun.common.utils.R;
import com.songlanyun.modules.group.entity.ProductGoodsGroupEntity;
import com.songlanyun.modules.shop.service.ShopService;
import com.songlanyun.modules.skus.entity.ProductSkusEntity;
import com.songlanyun.modules.skus.service.ProductSkusService;
import com.songlanyun.modules.user.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;



/**
 * 商家的商品规格表，创建了之后，对于已有的规格，在发布商品时只需要选择
 *
 * @author lubo
 * @email sunlightcs@gmail.com
 * @date 2020-11-03 15:46:49
 */
@RestController
@RequestMapping("admin/skus")
public class ProductSkusAdminController  extends AbstractController {
    @Autowired
    private ProductSkusService productSkusService;


    /**
     * 商家店铺的所有商品规格
     */
    @GetMapping("/all")
    //@RequiresPermissions("shop:skus:all")
    public R all(){
        List<Map<String, Object>> objs = productSkusService.getAllSku(this.getShopId());
        return R.ok().put("list", objs);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("shop:skus:info")
    public R infoByShopId(@PathVariable("id") Long id){
        ProductSkusEntity entity = productSkusService.getByShopIdAndId(this.getShopId(),id);
        return R.ok().put("data", entity);
    }


    /**
     * 保存
     */
    @SysLog("保存商品规格")
    @PostMapping("/create")
    //@RequiresPermissions("shop:skus:create")
    public R create(@RequestBody ProductSkusEntity entity){
        ProductSkusEntity obj = productSkusService.add(this.getShopId(),entity);
        return R.ok().put("data", obj);
    }

    /**
     * 修改
     */
    @SysLog("修改商品规格")
    @PutMapping("/update")
    //@RequiresPermissions("shop:skus:update")
    public R update(@RequestBody ProductSkusEntity entity){
        ProductSkusEntity obj = productSkusService.update(this.getShopId(),entity);
        return R.ok().put("data", obj);
    }

    /**
     * 删除
     */
    @SysLog("删除商品规格")
    @DeleteMapping("/delete/{id}")
    //@RequiresPermissions("shop:skus:delete")
    public R delete(@PathVariable("id") Long id){
        productSkusService.deleteByShopIdAndId(this.getShopId(),id);
        return R.ok();
    }

    /**
     * 第一级商品规格
     */
    @GetMapping("/top")
    //@RequiresPermissions("system:skus:info")
    public R first(){
        List<ProductSkusEntity> objs = productSkusService.getTopSku(this.getShopId());
        return R.ok().put("list", objs);
    }

    /**
     * 根据父级获取商品规格
     */
    @GetMapping(value = "/children/{parentId:[0-9]+}")
    //@RequiresPermissions("system:skus:info")
    public R getChilds(@PathVariable long parentId) {

        List<ProductSkusEntity> objs = productSkusService.getChildren(this.getShopId(),parentId);
        return R.ok().put("list", objs);
    }

    /**
     * 根据选择获取规格
     */
    @PostMapping(value = "/select")
    public R selectByIds(@RequestBody Long[] ids) {

        List<ProductSkusEntity> objs = productSkusService.selectByIds(this.getShopId(),ids);
        return R.ok().put("list", objs);
    }

}
